var app = getApp();
var dateTimePicker = require('../resource/js/dateTimePicker.js');

Page(Object.assign({},{
    data:{
        options : null,
        inited : false,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
        });
        var cid = options.cid ? options.cid : 0;
        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'addcardstart',cid:cid},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        card : res.data.data.card ? res.data.data.card : {iszs:1,isspay:1},
                        inited : true,
                    },function(){

                        var card = res.data.data.card;

                        var obj1 = dateTimePicker.dateTimePicker(2018,2030,card.start != 0 ? card.start : false);
                        var obj2 = dateTimePicker.dateTimePicker(2018,2030,card.end != 0 ? card.end : false);
                        var obj3 = dateTimePicker.dateTimePicker(2018,2030,card.usestart && card.usestart.length  >= 10  ? card.usestart : false);
                        var obj4 = dateTimePicker.dateTimePicker(2018,2030,card.useend && card.useend.length >= 10 ? card.useend : false);
                        obj1.dateTimeArray.pop();
                        obj1.dateTime.pop();
                        obj2.dateTimeArray.pop();
                        obj2.dateTime.pop();
                        obj3.dateTimeArray.pop();
                        obj3.dateTime.pop();
                        obj4.dateTimeArray.pop();
                        obj4.dateTime.pop();
                        self.setData({
                            'dateTimeArray.start': obj1.dateTimeArray,
                            'dateTimeArray.end': obj2.dateTimeArray,

                            'dateTime.start': obj1.dateTime,
                            'dateTime.end': obj2.dateTime,

                            'dateTimeArray.usestart': obj3.dateTimeArray,
                            'dateTimeArray.useend': obj3.dateTimeArray,

                            'dateTime.usestart': obj4.dateTime,
                            'dateTime.useend': obj4.dateTime,

                        });

                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }else{
                    app.com.alert( res.data.message,function(){
                        app.globalData.adminurl = app.com.getUrlArgs();
                        if( res.data.errno == 2 ){
                            wx.navigateTo({
                                url : res.data.data.url
                            });
                        }
                    });
                }
            });
        });
        wx.setNavigationBarTitle({
          title: '添加优惠券',
        });
    },
    inputvalue(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        var value = e.detail.value;
        if( type == 'rule' ){
            var id = e.currentTarget.dataset.id;
            var name = e.currentTarget.dataset.typea;
            for (let i = 0,il = self.data.card.rule; i < il.length; i++) {
                if( il[i].id == id ){
                    var key =  'card.rule['+i+'].'+name;
                    self.setData({
                        [key] : value
                    })
                }
            }
        }else{
            self.setData({
                ['card.'+type] : value,
            })
        }
    },
    deleterule(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        for (let i = 0,il = self.data.card.rule; i < il.length; i++) {
            if( il[i].id == id ){
                self.data.card.rule.splice(i,1);
            }
        }
        var isrule = 1;
        if( self.data.card.rule.length <= 0 ) var isrule = 0;

        self.setData({
            'card.isrule' : isrule,
            'card.rule' : self.data.card.rule,
        })
    },
    addshop(){
        var self = this;
        if( !self.data.card.shop ){
            self.data.card.shop = [];
        }
        self.data.card.shop.push({
            name : '',
            tel : '',
            add : ''
        });
        self.setData({
            'card.shop' : self.data.card.shop,
        })
    },
    deleteCard(e){
        var self = this;
        self.data.card.shop.splice(e.currentTarget.dataset.i,1);
        self.setData({
            'card.shop' : self.data.card.shop
        })
    }, 
    radio(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        var value = e.currentTarget.dataset.v;
        var key =  'card.'+type;
        self.setData({
            [key] : value,
            'card.usestart' : '',
            'card.useend' : '',
        })
    },
    pub(){
        var self = this;
        var card = self.data.card;

        if( !card.name ) {
            app.com.toast('请填入备注名称');return false;
        }
        self.savedata();
    },
    savedata(){
        var self = this;
        var form = JSON.stringify( self.data.card );
        app.com.http('admin','POST',{op:'pubcard',form:form},0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message,'success',function(){
                    if( !self.data.card.id ){
                        self.setData({
                            card : {},
                        });
                    }
                });
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });
    },
    changeDateTimeColumn1(e) {
        var self = this;
        var type = e.currentTarget.dataset.type;
        var value = e.detail.value;
        self.setData({ 
            ['dateTime.'+type]: e.detail.value,
            ['card.'+type] : self.data.dateTimeArray[type][0][value[0]]+'-'+self.data.dateTimeArray[type][1][value[1]]+'-'+self.data.dateTimeArray[type][2][value[2]]+' '+self.data.dateTimeArray[type][3][value[3]]+':'+self.data.dateTimeArray[type][4][value[4]]
        });
    },
    toaddress(){
        wx.chooseAddress({
            success: function (res) {}
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    
}))